﻿using System;
using Motoball.screens;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Motoball.screenmanager;
using System.IO;
using System.Xml.Serialization;

namespace Motoball
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Motoball : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        public static ScreenManager screenManager;
        public static Camera camera { get; protected set; }
        private ContentStorage storage;

        public BloomComponent bloom;


        public static InputState input;


        public Motoball()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            //graphics.IsFullScreen = true;
            
            //graphics.PreferredBackBufferWidth = 720;
            //graphics.PreferredBackBufferHeight = 405;
            //graphics.ApplyChanges();
            // graphics.IsFullScreen = true;
            // graphics.PreferMultiSampling = true;

            graphics.MinimumPixelShaderProfile = ShaderProfile.PS_2_0;
            screenManager = new ScreenManager(this);

            camera = new Camera(this, new Vector3(0, 1200, 0), new Vector3(0, 0, 1), Vector3.Up);
            bloom = new BloomComponent(this);
            bloom.content = Content;
            

            Components.Add(camera);
            Components.Add(screenManager);

            storage = new ContentStorage();
            input = new InputState();
            //graphics.SynchronizeWithVerticalRetrace = false;
            //IsFixedTimeStep = false;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            ContentStorage.screenHeight = graphics.GraphicsDevice.PresentationParameters.BackBufferHeight;
            ContentStorage.screenWidth = graphics.GraphicsDevice.PresentationParameters.BackBufferWidth;
            base.Initialize();
            

            bloom.Initialize();


            AudioManager.Initialize(this, @"Content\Audio\Motoball_Audio.xgs",
                  @"Content\Audio\Wave Bank.xwb", @"Content\Audio\Sound Bank.xsb");
//#if !XBOX
//            AudioManager.Initialize(this, @"Content\Audio\Win\Motball_Audio.xgs",
//                  @"Content\Audio\Win\Wave Bank.xwb", @"Content\Win\Audio\Sound Bank.xsb");
//#else
            
//             AudioManager.Initialize(this, @"Content\Audio\Xbox\Motball_Audio.xgs",
//                 @"Content\Audio\Win\Wave Bank.xwb", @"Content\Audio\Xbox\Sound Bank.xsb");
//#endif
            


            bloom.Settings = new BloomSettings("Default", 0.25f, 4, 1, 1);

           
        }
       
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();
        }
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            input.Update(gameTime);
            base.Update(gameTime);

            //if (input.IsKeyPress(Keys.Y))
            //{
            //    ContentStorage.bloomIntensity += 0.01f;
            //    bloom.Settings = new BloomSettings("Default", 0.25f, 4, ContentStorage.bloomIntensity, ContentStorage.baseIntensity, 1, 1);
            //}
            //if (input.IsKeyPress(Keys.C))
            //{
            //    ContentStorage.bloomIntensity -= 0.01f;
            //    bloom.Settings = new BloomSettings("Default", 0.25f, 4, ContentStorage.bloomIntensity, ContentStorage.baseIntensity, 1, 1);

            //}
            //if (input.IsKeyPress(Keys.V))
            //{
            //    ContentStorage.baseIntensity += 0.01f;
            //    bloom.Settings = new BloomSettings("Default", 0.25f, 4, ContentStorage.bloomIntensity, ContentStorage.baseIntensity, 1, 1);

            //}
            //if (input.IsKeyPress(Keys.B))
            //{
            //    ContentStorage.baseIntensity -= 0.01f;
            //    bloom.Settings = new BloomSettings("Default", 0.25f, 4, ContentStorage.bloomIntensity, ContentStorage.baseIntensity, 1, 1);

            //}


        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {


            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }

        public InputState Input
        {
            get { return input; }
        }

        #if !XBOX
        public static SaveGameStorage savegame = new SaveGameStorage();
#endif
        

    }
#if !XBOX
    [Serializable]
    public struct SaveGame
    {
        public string[] items;

        [NonSerialized]
        public string fileName;
        public int itemCount;
    }

    public class SaveGameStorage
    {

        
         public void Save(SaveGame sg)
        {
             
            IAsyncResult result = Guide.BeginShowStorageDeviceSelector(PlayerIndex.One, null, null);
            while (!result.IsCompleted)
            { }
            if (result.IsCompleted)
            {
                StorageDevice device = Guide.EndShowStorageDeviceSelector(result);

                // Open a storage container
                StorageContainer container = device.OpenContainer("TestStorage");

                // Get the path of the save game
                string name = sg.fileName + ".xml";
                string filename = Path.Combine(container.Path, name);

                // Open the file, creating it if necessary
                FileStream stream = File.Open(filename, FileMode.OpenOrCreate);

                // Convert the object to XML data and put it in the stream
                XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
                serializer.Serialize(stream, sg);

                // Close the file
                stream.Close();

                // Dispose the container, to commit changes
                container.Dispose();
            }
        }


    }
#endif
}
